package com.hgd.security.evp.swagger.config;

import com.fasterxml.classmate.ResolvedType;
import com.google.common.base.Optional;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.schema.ModelReference;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.DocumentationCache;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @author By 尚
 * @date 2019/4/2 18:08
 */
@Configuration
public class Swagger2Config implements CommandLineRunner {
    @Autowired
    private DocumentationCache documentationCache;

    @Bean
    public Docket createRestApi()  {
        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            //这里采用包扫描的方式来确定要显示的接口
            .apis(RequestHandlerSelectors.basePackage("com.hgd.security.evp.swagger.controller"))
            .paths(PathSelectors.any())
            .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("Docker Operation Rest Api Doc")
            .description("Docker Operation Api文档")
            //                .termsOfServiceUrl("http://git.oschina.net/naan1993/datapaas")
            .version("1.0")
            .build();
    }

    @Override
    public void run(String... strings) throws Exception {
        Documentation aDefault = documentationCache.documentationByGroup("default");

        Multimap<String, ApiListing> apiListings = aDefault.getApiListings();
        Collection<ApiListing> apiListings1 = apiListings.get("test-controller");
        for (ApiListing apiListing : apiListings1) {
            List<ApiDescription> apis = apiListing.getApis();
            for (ApiDescription api : apis) {
                List<Operation> operations = api.getOperations();
                for (Operation operation : operations) {
                    List<Parameter> parameters = operation.getParameters();
                    /*
                    String name,
      String description,
      String defaultValue,
      boolean required,
      boolean allowMultiple,
      Boolean allowEmptyValue,
      ModelReference modelRef,
      Optional<ResolvedType> type,
      AllowableValues allowableValues,
      String paramType,
      String paramAccess,
      boolean hidden,
      String pattern,
      String collectionFormat,
      int order,
      Object scalarExample,
      Multimap<String, Example> examples,
      List<VendorExtension> vendorExtensions
                     */
                    ModelReference modelRef=new ModelRef("java.util.Map");

                    Optional<ResolvedType> type = Optional.absent();
                    Parameter newParameter=new Parameter(
                        "newName",
                        "newDescription",
                        null,
                        true,
                        false,
                        null,
                        modelRef,
                        type,
                        null,
                        "body",
                        null,
                        false,
                        null,
                        null,
                        2147483647,
                        null,
                        LinkedListMultimap.create(),
                        new ArrayList<>()
                    );

                    parameters.add(newParameter);
                    Set<ResponseMessage> responseMessages = operation.getResponseMessages();
                    ModelReference responseModel = operation.getResponseModel();

                }
            }
        }
    }
}

